using System;

namespace FwkLight.Domain.Fetching
{
  public interface IRootFetchingPartInHierarchy : ICalculator<IFetchingPart, IFetchingPart>
  {
  }

  public class RootFetchingPartInHierarchy : IRootFetchingPartInHierarchy
  {
    public IFetchingPart Calculate(IFetchingPart fetchingPart)
    {
      if (fetchingPart.IsRoot)
        return fetchingPart;

      IFetchingPart parent = fetchingPart.Parent;
      while (!parent.IsRoot)
        parent = parent.Parent;
      return parent;
    }
  }
}